local DIY = require "packages.diy_utility.diy_utility"

local zhuanmi = fk.CreateSkill {
  name = "rmt__zhuanmi",
}

Fk:loadTranslationTable{
  ["rmt__zhuanmi"] = "赚米",
  [":rmt__zhuanmi"] = "出牌阶段限一次，你可以移去中央区的X张牌。若中央区因此清空，你摸X张牌（X为你本回合执行的出牌阶段数）。",
  ["#rmt__zhuanmi"] = "赚米：移去中央区的%arg张牌，若清空中央区则摸等量牌",
}

zhuanmi:addEffect("active", {
  anim_type = "drawcard",
  prompt = function (self, player, selected_cards, selected_targets)
    return "#rmt__zhuanmi:::"..player:getMark("rmt__zhuanmi-turn")
  end,
  target_num = 0,
  target_filter = Util.FalseFunc,
  card_num = function (self, player) ---@class integer
    return player:getMark("rmt__zhuanmi-turn")
  end,
  expand_pile = function (self)
    return Fk:currentRoom():getBanner("@$CenterArea")
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected < player:getMark("rmt__zhuanmi-turn") and table.contains(Fk:currentRoom():getBanner("@$CenterArea"), to_select)
  end,
  can_use = function (self, player)
    return player:usedSkillTimes(zhuanmi.name, Player.HistoryPhase) == 0 and
    player:getMark("rmt__zhuanmi-turn") > 0 and
    #Fk:currentRoom():getBanner("@$CenterArea") >= player:getMark("rmt__zhuanmi-turn")
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    DIY.removeCenterCards(player, effect.cards)
    if #room:getBanner("@$CenterArea") == 0 and not player.dead then
      player:drawCards(#effect.cards, zhuanmi.name)
    end
  end,
})

zhuanmi:addEffect(fk.StartPlayCard, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(zhuanmi.name, true)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local n = #room.logic:getEventsOfScope(GameEvent.Phase, 999, function(e)
      return e.data.who == player and e.data.phase == Player.Play
    end, Player.HistoryTurn)
    room:setPlayerMark(player, "rmt__zhuanmi-turn", n)
  end,
})

zhuanmi:addAcquireEffect(function (self, player)
  player.room:addSkill("#CenterArea")
end)

return zhuanmi
